home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 1 (Walnut Creek)
/
Aminet - June 1993 [Walnut Creek].iso
/
usenet
/
sources
/
volume90
/
util
/
wrap_1.00
/
Wrap.c
next >
Wrap
C/C++ Source or Header
|
1990-03-04
|
9KB
|
306 lines
#include <exec/types.h>
#include <exec/exec.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <ctype.h>
#define CR 0x0D
#define LF 0x0A
UBYTE GetWord();
void SetVariables();
BYTE DoubleSpacing = FALSE;
USHORT RightMargin = 0;
USHORT LeftMargin = 0;
USHORT TopMargin = 0;
USHORT BottomMargin = 0;
SHORT PageSize = 66;
USHORT TabSpaces = 5;
USHORT ParaIndent = 0;
main(argc,argv)
int argc;
char *argv[];
{
struct FileHandle *infile,*outfile; /*pointers to files*/
char chip outbuf[256];
UBYTE EOP=FALSE; /*End of paragraph flag*/
UBYTE EOF=FALSE; /*End of file flag*/
UBYTE wordcount,linecount;
ULONG Line;
char chip LeftSpaces[80];
char chip TopReturns[80];
char chip IndentSpaces[80];
char chip BottomReturns[80];
LeftSpaces[0]=0x0a;
LeftSpaces[1]=NULL;
TopReturns[0]=BottomReturns[0]=NULL;
if(argv[1][0]=='?') /*Print the instructions*/
{
puts("Wrap V1.0 (c)1989, 1990 by Dave Schreiber. All rights reserved.");
puts("Written by Dave Schreiber.");
puts("Format:");
puts(" 1> wrap [options] Infile Outfile");
puts(" Infile-text file to be processed");
puts(" Outfile-filename that the formatted text is to be written to");
puts(" Options (in upper- or lowercase");
puts(" -a# -- number of spaces to expand tabs out to (default 5)");
puts(" -b# -- bottom margin (default 0)");
puts(" -t# -- top margin (default 0)");
puts(" -r# -- right margin (default 0)");
puts(" -l# -- left margin (default 0)");
puts(" -i# -- number of spaces to indent each paragraph (default 0)");
puts(" -p# -- page size (with no margin, default 66)");
puts(" -d -- double spaced (default is single spaced)");
exit(0);
}
if((argv[argc-1][0]=='-') || (argc == 2)) /*Check for output filename*/
{
puts("No output file specified!!!\n");
exit(100);
}
if((argv[argc-2][0]=='-') || (argc == 1)) /*Check for input filename*/
{
puts("No input file specified!!!\n");
exit(200);
}
/*Get the command line switches, etc.*/
SetVariables(argc,argv,LeftSpaces,TopReturns,BottomReturns,IndentSpaces);
if(79-RightMargin-LeftMargin < 1)
{
puts("The left/right margins are too large!\n");
exit(300);
}
/*Get the true page size (after the margins are taken out)*/
PageSize = PageSize - (TopMargin + BottomMargin);
if(PageSize < 1)
{
puts("The top/bottom margins are too large!");
exit(400);
}
/*Open read file*/
if((infile=(struct FileHandle *)Open(argv[argc-2],MODE_OLDFILE))==NULL)
{
puts("Couldn't open the read file!");
exit(500);
}
/*Now go for write file*/
if((outfile=(struct FileHandle *)Open(argv[argc-1],MODE_NEWFILE))==NULL)
{
puts("Couldn't open the write file!");
Close(infile); /*Clean up*/
exit(600);
}
/*Write the first top margin padding*/
Write(outfile,&TopReturns,TopMargin);
/*Get the first word*/
linecount=wordcount=GetWord(outbuf,&EOP,&EOF,infile);
/*Write out the left margin*/
Write(outfile,&LeftSpaces[1],LeftMargin);
/*Write out the paragraph indent*/
Write(outfile,IndentSpaces,ParaIndent);
linecount+=ParaIndent;
/*Write the first word*/
Write(outfile,outbuf,wordcount);
wordcount=(linecount+=LeftMargin);
/*First line of the page...*/
Line = 0;
while(!EOF) /*While there's still stuff to be processed...*/
{
outbuf[1]=NULL;
wordcount=GetWord(&outbuf[1],&EOP,&EOF,infile)+1; /*Get a word*/
if((linecount+=wordcount) > 79-RightMargin) /*End of line*/
{
if(DoubleSpacing)
{
Line++;
outbuf[0]=0x0a; /*If double spaced, write out an*/
Write(outfile,outbuf,1); /*extra line*/
}
if(++Line == PageSize) /*If we're at the end of a page...*/
{
Write(outfile,BottomReturns,BottomMargin); /*Write out the margins*/
Write(outfile,TopReturns,TopMargin);
Line = 0; /*And start a new page*/
}
Write(outfile,LeftSpaces,LeftMargin+1); /*Left margin*/
Write(outfile,&outbuf[1],wordcount-1); /*Write the word*/
linecount=wordcount+LeftMargin;
}
else /*If we're not at the end of a line*/
{
if(linecount != (wordcount+LeftMargin)) /*If were not at the start*/
{ /*of a line...*/
outbuf[0]=0x20; /*Tack on a space*/
Write(outfile,outbuf,wordcount);
}
else /*Don't tack on a space (insures new paragraphs are*/
Write(outfile,&outbuf[1],wordcount-1); /*formatted properly)*/
}
if(EOP) /*If we're at the end of a paragraph*/
{
outbuf[0]=0x0a; /*Start a new line*/
Write(outfile,outbuf,1);
if(!EOF) /*Do this only if we're not at the end of the text*/
{
if(DoubleSpacing) /*Another line if double spaced*/
{
Line++;
Write(outfile,outbuf,1);
}
if(++Line == PageSize) /*If at the end of a page?*/
{ /*Write the margins, etc.*/
Write(outfile,BottomReturns,BottomMargin);
Write(outfile,TopReturns,TopMargin);
Line = 0;
}
if(LeftMargin>0) /*Write out a margin if it exists*/
Write(outfile,&LeftSpaces[1],LeftMargin-1);
linecount=LeftMargin;
if(ParaIndent>0) /*Write out the indent for the start*/
Write(outfile,IndentSpaces,ParaIndent-1); /*of the*/
/*next paragraph*/
linecount+=ParaIndent;
}
}
}
Close(outfile); /*Done*/
Close(infile);
exit(0);
}
UBYTE GetWord(buffer,EOP,EOF,in)
char *buffer;
UBYTE *EOP,*EOF;
struct FileHandle *in;
{
char chip inbuf[8];
UBYTE c,wordcount=0;
*EOP=FALSE;
*EOF=GetByte(inbuf,in);
while ( ( !(*EOF) ) && (inbuf[0] != 0x0a) && (inbuf[0] != 0x20) )
{
if(inbuf[0]=='\t') /*Expand tab out to spaces*/
for(c=0;c<TabSpaces;buffer[c++]=' ',wordcount++);
else
buffer[wordcount++]=inbuf[0]; /*Just stick it in otherwise*/
*EOF=GetByte(inbuf,in);
}
if(inbuf[0]==0x0a || *EOF) /*If we're at the end of a paragraph...*/
*EOP=TRUE;
return(wordcount);
}
/*This routine reads a small chuck (128) of a file at once and then*/
/*lets the calling function get it a byte at a time. Why not just */
/*read off the disk a byte at a time? Speed. For example, on my */
/*hard drive equipped system, this speeds up processing 400% */
GetByte(buffer,infile)
char *buffer;
struct FileHandle *infile;
{
static char chip Buffer[128];
static curpos=0; /*Next byte to be returned*/
static end = 0; /*Last byte in buffer*/
if(curpos == end) /*If we've sent every byte*/
{ /*Get another 8K chunk*/
curpos = 0;
end=Read(infile,Buffer,sizeof(Buffer));
}
buffer[0]=Buffer[curpos++];
return(!end); /*Returns whether or not EOF*/
}
void SetVariables(argc,argv,LeftSpaces,TopReturns,BottomReturns,
IndentSpaces)
int argc;
char *argv[];
char *LeftSpaces,*TopReturns,*BottomReturns,*IndentSpaces;
{
register BYTE c,s;
int value;
for(c=1;c<argc-2;c++)
{
switch(argv[c][1]) /*Get each switch*/
{
case 'd': /*Double spacing*/
case 'D': /*(Lowercase and uppercase, just to be extra friendly)*/
DoubleSpacing=TRUE;
break;
case 'r':
case 'R': /*Right margin*/
stcd_i(&argv[c][2],&value);
RightMargin = value;
break;
case 'l':
case 'L': /*Left margin*/
stcd_i(&argv[c][2],&value);
LeftMargin = value;
for(s=0;s < LeftMargin;LeftSpaces[++s]=' ');
LeftSpaces[s+1]=NULL;
break;
case 't': /*Set top margin*/
case 'T':
stcd_i(&argv[c][2],&value);
TopMargin = value;
for(s=0;s < TopMargin;TopReturns[s++]=0x0a);
TopReturns[s]=NULL;
break;
case 'b': /*Set the bottom margin*/
case 'B':
stcd_i(&argv[c][2],&value);
BottomMargin = value;
for(s=0;s < BottomMargin;BottomReturns[s++]=0x0a);
BottomReturns[s]=NULL;
break;
case 'p': /*Set the page size*/
case 'P':
stcd_i(&argv[c][2],&value);
PageSize = value;
break;
case 'a': /*Set the number of spaces a TAB represents*/
case 'A':
stcd_i(&argv[c][2],&value);
TabSpaces = value;
break;
case 'i':
case 'I':
stcd_i(&argv[c][2],&value);
ParaIndent = value;
for(s=0;s < ParaIndent && s < 80;IndentSpaces[s++]=' ');
IndentSpaces[s]=NULL;
break;
}
}
}